package com.gzsxy.esjy.gateway.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.List;
import java.util.Optional;

/**
 * @author xiaolong
 * @version 1.0
 * @description: swagger聚合接口，三个接口都是swagger-ui.html需要访问的接口
 * @date 2021/11/15 20:10
 */
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerController {

    @Autowired(required = false)
   private SecurityConfiguration securityConfiguration;

    @Autowired(required = false)
    private UiConfiguration uiConfiguration;

    private final SwaggerResourcesProvider swaggerResources;


    @Autowired
    public SwaggerController(SwaggerResourcesProvider swaggerResources){
        this.swaggerResources = swaggerResources;
    }


    /**
     * Swagger安全配置，支持oauth和apiKey设置
     */
    @RequestMapping(value = "/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()),HttpStatus.OK));
    }

    /**
     * Swagger UI配置
     */
    @RequestMapping(value = "/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()),HttpStatus.OK));
    }

    /**
     * Swagger资源配置，微服务中这各个服务的api-docs信息
     */
    @RequestMapping
    public ResponseEntity<List<SwaggerResource>> swaggerResources() {
        return new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK);
    }



}
